<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/31
 * Time: 14:53
 */

namespace app\common\model;


class Http
{
    static public function post($url, array $data){
        if(empty($url)){
            return err('url参数不能为空');
        }
        if(!match($url,'/^https?:\/\/.*/')){
            return err('地址格式不正确');
        }
        try{
            //$rs=self::http($url,$data,'POST','Content-Type:application/x-www-form-urlencoded;charset=UTF-8',false);
            $rs=http($url,$data,'POST',array("Content-Type:application/x-www-form-urlencoded;charset=UTF-8"));
            $arr=json_decode($rs,true);
            if(is_array($arr)){
                $ret['data']=$arr;
            }else{
                $ret['data']=$rs;
            }
            $ret['result']=1;
        }catch(\Exception $e){
            $ret['result']=0;
            $ret['msg']=$e->getMessage();
        }
            return $ret;

    }
    static function post_json($url, $data_string) {
        if(empty($url)){
            return err('url参数不能为空');
        }
        if(!match($url,'/^https?:\/\/.*/')){
            return err('地址格式不正确');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Content-Length: '.strlen($data_string)));
        ob_start();
        try{
            $rs =curl_exec($ch);
            $arr=json_decode($rs,true);
            if(is_array($arr)){
                $ret['data']=$arr;
            }else{
                $ret['data']=$rs;
            }
            $ret['result']=1;
        }catch(\Exception $e){
            $ret['result']=0;
            $ret['msg']=$e->getMessage();
        }

        ob_end_clean();
        curl_close($ch);
        return $ret;

    }
    static public function get($url, array $data=[]){
        if(empty($url)){
            return err('url参数不能为空');
        }
        if(!match($url,'/^https?:\/\/.*/')){
            return err('地址格式不正确');
        }
        try{
            $rs=http($url,$data,'GET',array("Content-Type:application/x-www-form-urlencoded;charset=UTF-8"));
            $arr=json_decode($rs,true);
            if(is_array($arr)){
                $ret['data']=$arr;
            }else{
                $ret['data']=$rs;
            }
            $ret['result']=1;
        }catch(\Exception $e){
            $ret['result']=0;
            $ret['msg']=$e->getMessage();
        }
        return $ret;

    }
    static public function https_post($url, array $query_data) {
        if(empty($url)){
            return err('url参数不能为空');
        }
        if(!match($url,'/^https?:\/\/.*/')){
            return err('地址格式不正确');
        }
        $post_str = json_encode($query_data);

        $curl = curl_init(); // 初始化curl
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 显示输出结果
        curl_setopt($curl, CURLOPT_POST, 1); // post传输数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_str); // post传输数据
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3); // 设置等待时间
        $header = array(
            "Content-Type:application/x-www-form-urlencoded;charset=UTF-8"
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //https 请求
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        try{
            $rs = curl_exec($curl);
            $arr=json_decode($rs,true);
            if(is_array($arr)){
                $ret['data']=$arr;
            }else{
                $ret['data']=$rs;
            }
            $ret['result']=1;
        }catch(\Exception $e){
            $ret['result']=0;
            $ret['msg']=$e->getMessage();
        }

        curl_close($curl);

        return $ret;
    }

}